﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Configuration;

namespace ICarPool.DBCommon
{
    public static class DBUtility
    {
        /// <summary>
        /// 获取配置文件中默认数据连接字符串：connectionStrings\ConString
        /// </summary>
        /// <returns></returns>
        public static String GetConnectionString()
        {
            return GetConnectionStringByKey("ConString");
        }

        /// <summary>
        /// 根据节点读取配置文件中的数据库连接串
        /// </summary>
        /// <param name="key">节点名字</param>
        /// <returns>数据库连接串</returns>
        public static String GetConnectionStringByKey(string key)
        {
            string conString = string.Empty;

            try
            {
                conString = ConfigurationManager.ConnectionStrings[key].ToString();
                return conString;
            }
            catch (Exception)
            {
                throw new Exception(string.Format(@"配置文件中：connectionStrings\{0} 节点未配置，或者配置错误，请检查。",key));
            }

        }

        /// <summary>
        /// 根据Key 获取appSetting下配置节点
        /// </summary>
        /// <param name="key">节点名称</param>
        /// <returns></returns>
        public static String GetAppSettingByKey(string key)
        {
            string appKey = string.Empty;

            try
            {
                appKey = ConfigurationManager.AppSettings[key].ToString();
                return appKey;
            }
            catch (Exception)
            {
                throw new Exception(string.Format(@"配置文件中：appSettings\{0} 节点未配置，或者配置错误，请检查。", key));
            }
        }
    }
}
